# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms_hi.dm.dm_taking_timely_platform_summary_hi import jms_dm__dm_taking_timely_platform_summary_hi

label = f"dm_taking_timely_platform_summary_hi_{int(time())}"
timeout = timedelta(minutes=60).seconds
doris_jms_dm__dm_taking_timely_platform_summary_hi = RichSqlSensor(
    task_id='doris_jms_dm__dm_taking_timely_platform_summary_hi',
    pool='broker_load_pool',
    email=['suning@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='doris',
    pre_sql=f"""
    TRUNCATE TABLE jms_dm.dm_taking_timely_platform_summary_hi PARTITION (
         p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+0) | cst_ds_nodash }}}}
        ,p{{{{ execution_date | date_add(+1) | cst_ds_nodash }}}}
    );
    LOAD LABEL jms_dm.{label} (
        DATA INFILE(
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_platform_summary_hi/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_platform_summary_hi/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_platform_summary_hi/dt={{{{ execution_date | date_add(+0) | cst_ds }}}}/*",
            "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_taking_timely_platform_summary_hi/dt={{{{ execution_date | date_add(+1) | cst_ds }}}}/*"
        )
        INTO TABLE dm_taking_timely_platform_summary_hi
        FORMAT AS 'PARQUET'(
             sender_network_code
            ,sender_network_name
            ,sender_fran_code
            ,sender_fran_name
            ,sender_agent_code
            ,sender_agent_name
            ,sender_region_code
            ,sender_region_name
            ,thd_all_cnt
            ,thd_timely_cnt
            ,thd_untimely_cnt
            ,zjs_all_cnt
            ,zjs_timely_cnt
            ,zjs_untimely_cnt
            ,xyf_all_cnt
            ,xyf_timely_cnt
            ,xyf_untimely_cnt
            ,qxt_all_cnt
            ,qxt_timely_cnt
            ,qxt_untimely_cnt
            ,jdw_all_cnt
            ,jdw_timely_cnt
            ,jdw_untimely_cnt
            ,qt_all_cnt
            ,qt_timely_cnt
            ,qt_untimely_cnt
            ,all_cnt
            ,timely_cnt
            ,untimely_cnt
            ,xyf_timely_qz_cnt 
            ,qxt_timely_qz_cnt 
        )
        columns from path as (dt)
        set(
            sender_network_code  =   sender_network_code
           ,sender_network_name  =   sender_network_name
           ,sender_fran_code     =   sender_fran_code
           ,sender_fran_name     =   sender_fran_name
           ,sender_agent_code    =   sender_agent_code
           ,sender_agent_name    =   sender_agent_name
           ,sender_region_code   =   sender_region_code
           ,sender_region_name   =   sender_region_name
           ,thd_all_cnt          =   thd_all_cnt
           ,thd_timely_cnt       =   thd_timely_cnt
           ,thd_untimely_cnt     =   thd_untimely_cnt
           ,zjs_all_cnt          =   zjs_all_cnt
           ,zjs_timely_cnt       =   zjs_timely_cnt
           ,zjs_untimely_cnt     =   zjs_untimely_cnt
           ,xyf_all_cnt          =   xyf_all_cnt
           ,xyf_timely_cnt       =   xyf_timely_cnt
           ,xyf_untimely_cnt     =   xyf_untimely_cnt
           ,qxt_all_cnt          =   qxt_all_cnt
           ,qxt_timely_cnt       =   qxt_timely_cnt
           ,qxt_untimely_cnt     =   qxt_untimely_cnt
           ,jdw_all_cnt          =   jdw_all_cnt
           ,jdw_timely_cnt       =   jdw_timely_cnt
           ,jdw_untimely_cnt     =   jdw_untimely_cnt
           ,qt_all_cnt           =   qt_all_cnt
           ,qt_timely_cnt        =   qt_timely_cnt
           ,qt_untimely_cnt      =   qt_untimely_cnt
           ,all_cnt              =   all_cnt
           ,timely_cnt           =   timely_cnt
           ,untimely_cnt         =   untimely_cnt
           ,xyf_timely_qz_cnt    =   xyf_timely_qz_cnt
           ,qxt_timely_qz_cnt    =   qxt_timely_qz_cnt
           ,dt                   =   dt
       )
    )
    WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
    PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

doris_jms_dm__dm_taking_timely_platform_summary_hi << jms_dm__dm_taking_timely_platform_summary_hi